home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Recursive Shell 1.0.1 / Examples / Count Items ƒ / Main.c < prev    next >
C/C++ Source or Header  |  1996-06-14  |  12KB  |  534 lines

  1. /*****************************************************************************/
  2. //
  3. //    Main.c
  4. //
  5. //    Main source code for recursive shell, which is the basis for any variety
  6. //    of drag-and-drop utilities which recursively do something to entire
  7. //    folders of files and/or folders.
  8. //
  9. //    Version 1.0
  10. //
  11. //    Created:    13 May 1996
  12. //    Modified:    14 June 1996
  13. //
  14. /*****************************************************************************/
  15.  
  16.  
  17. //    Interfaces
  18.  
  19. #include    "Main.h"
  20. #include    "GetFolder.h"
  21. #include    "DialogUtil.h"
  22. #include    "SearchFolder.h"
  23. #include    "Utility.h"
  24. #include    "DoStuff.h"
  25.  
  26.  
  27. //    Definitions
  28.  
  29. #define    kSleep            32767
  30. #define    kGestaltMask    1L
  31.  
  32.  
  33. //    Globals
  34.  
  35. Boolean        gDone, gRecurseDir;
  36.  
  37.  
  38. //    Prototypes
  39.  
  40. void    main( void );
  41. void    ToolBoxInit( void );
  42. void    MenuBarInit( void );
  43. void    EventInit( void );
  44.  
  45. pascal OSErr DoOpenApp( AppleEvent theAppleEvent, AppleEvent reply, long refCon );
  46. pascal OSErr DoOpenDoc( const AppleEvent *theAppleEvent, const AppleEvent *reply, long refCon );
  47. pascal OSErr DoPrintDoc( AppleEvent theAppleEvent, AppleEvent reply, long refCon );
  48. pascal OSErr DoQuitApp( AppleEvent theAppleEvent, AppleEvent reply, long refCon );
  49.  
  50. void    EventLoop( void );
  51. void    DoEvent( EventRecord *eventPtr );
  52. void    HandleMouseDown( EventRecord *eventPtr );
  53. void    HandleAppleChoice( short item );
  54. void    HandleFileChoice( short item );
  55.  
  56. void    SelectFolder( void );
  57. void    DoFileSelection( FSSpec selSpec );
  58. void    DoFolderSelection( FSSpec selSpec );
  59.  
  60.  
  61. //
  62. //    Main
  63. //
  64. void    main( void )
  65. {
  66.     ToolBoxInit();
  67.         
  68.     MenuBarInit();
  69.     
  70.     //    Check for System 7 or higher (since we use a lot of 7.x features):
  71.     if ( !SysSevenOrBetter() )
  72.         DisplayFatalError( "\pThis program requries System 7.0 or better." );
  73.     
  74.     EventInit();
  75.     
  76.     EventLoop();
  77. }
  78.  
  79.  
  80. /*****************************************************************************/
  81. //    Initialization Routines
  82. /*****************************************************************************/
  83.  
  84. //
  85. //    ToolBoxInit
  86. //
  87. void    ToolBoxInit( void )
  88. {
  89.     InitGraf( &qd.thePort );
  90. /*    InitGraf( &thePort );    */    //    This is how it's done in Think C...
  91.     InitFonts();
  92.     InitWindows();
  93.     InitMenus();
  94.     TEInit();
  95.     InitDialogs( nil );
  96.     InitCursor();
  97. }
  98.  
  99.  
  100. //
  101. //    MenuBarInit
  102. //
  103. void    MenuBarInit( void )
  104. {
  105.     Handle        menuBar;
  106.     MenuHandle    menu;
  107.     
  108.     menuBar = GetNewMBar( kBaseResID );    //    load the menubar resource
  109.     if ( menuBar == nil )
  110.         DisplayFatalError( "\pERROR: could not load menu resource!" );
  111.     
  112.     SetMenuBar( menuBar );                //    make it the current menubar
  113.     
  114.     menu = GetMHandle( mApple );        //    get a handle to the apple menu
  115.     AddResMenu( menu, 'DRVR' );            //    add the desk accessories to the Apple menu
  116.     
  117.     DrawMenuBar();                        //    draw the menu bar
  118. }
  119.  
  120.  
  121. //
  122. //    EventInit
  123. //
  124. void    EventInit( void )
  125. {
  126.     OSErr                err;
  127.     long                feature;
  128.     AEEventHandlerUPP    handlerUPP;
  129.  
  130.     //    Check Gestalt to see if AppleEvents are available:
  131.     err = Gestalt( gestaltAppleEventsAttr, &feature );
  132.     if ( err != noErr )
  133.         DisplayFatalError( "\pFATAL ERROR: Problem calling Gestalt. This program requires System 7 or higher." );
  134.     if ( !( feature & ( kGestaltMask << gestaltAppleEventsPresent ) ) )
  135.         DisplayFatalError( "\pFATAL ERROR: AppleEvents not available. This program requires System 7 or higher." );
  136.  
  137.     //    Install all the appropriate handlers:
  138.     handlerUPP = NewAEEventHandlerProc( DoOpenApp );
  139.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, handlerUPP, 0L, false );
  140.     if ( err != noErr )
  141.         DisplayAlert( "\pERROR: Open Application AppleEvent not available." );
  142.     
  143.     handlerUPP = NewAEEventHandlerProc( DoOpenDoc );
  144.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, handlerUPP, 0L, false );
  145.     if ( err != noErr )
  146.         DisplayAlert( "\pERROR: Open Document AppleEvent not available." );
  147.     
  148.     handlerUPP = NewAEEventHandlerProc( DoPrintDoc );
  149.     err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, handlerUPP, 0L, false );
  150.     if (err != noErr )
  151.         DisplayAlert( "\pError: kAEPrintDocuments Apple Event not available" );
  152.     
  153.     handlerUPP = NewAEEventHandlerProc( DoQuitApp );
  154.     err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, handlerUPP, 0L, false );
  155.     if ( err != noErr )
  156.         DisplayAlert( "\pError: kAEQuitApplication Apple Event not available" );
  157. }
  158.  
  159.  
  160.  
  161. /*****************************************************************************/
  162. //    Menu-managing Routines
  163. /*****************************************************************************/
  164.  
  165. //
  166. //    WorkMenus
  167. //    The menus as shown while the program is recursing through files/folders.
  168. //    The "Open..." item is changed to "Abort" and everything else is disabled.
  169. //
  170. void    WorkMenus( void )
  171. {
  172.     MenuHandle    myMenu;
  173.     
  174.     myMenu = GetMenuHandle( mApple );
  175.     if ( myMenu != nil )
  176.         DisableItem( myMenu, iAbout );
  177.     
  178.     myMenu = GetMenuHandle( mFile );
  179.     if ( myMenu != nil )
  180.     {
  181.         SetMenuItemText( myMenu, iOpen, "\pAbort" );
  182.         SetItemCmd( myMenu, iOpen, '.' );
  183.         EnableItem( myMenu, iOpen );
  184.         DisableItem( myMenu, iPrefs );
  185.         DisableItem( myMenu, iQuit );
  186.     }
  187.     
  188.     myMenu = GetMenuHandle( mEdit );
  189.     if ( myMenu != nil )
  190.     {
  191.         DisableItem( myMenu, iUndo );
  192.         DisableItem( myMenu, iCut );
  193.         DisableItem( myMenu, iCopy );
  194.         DisableItem( myMenu, iPaste );
  195.         DisableItem( myMenu, iClear );
  196.     }
  197. }
  198.  
  199.  
  200. //
  201. //    WaitMenus
  202. //    The menus as shown while the program is waiting.
  203. //
  204. void    WaitMenus( void )
  205. {
  206.     MenuHandle    myMenu;
  207.     
  208.     myMenu = GetMenuHandle( mApple );
  209.     if ( myMenu != nil )
  210.         EnableItem( myMenu, iAbout );
  211.     
  212.     myMenu = GetMenuHandle( mFile );
  213.     if ( myMenu != nil )
  214.     {
  215.         SetMenuItemText( myMenu, iOpen, "\pOpen..." );
  216.         SetItemCmd( myMenu, iOpen, 'O' );
  217.         EnableItem( myMenu, mFile );
  218.         DisableItem( myMenu, iPrefs );    //    Preferences not implemented yet
  219.         EnableItem( myMenu, iQuit );
  220.     }
  221.     
  222.     myMenu = GetMenuHandle( mEdit );
  223.     if ( myMenu != nil )
  224.     {
  225.         DisableItem( myMenu, iUndo );
  226.         DisableItem( myMenu, iCut );
  227.         DisableItem( myMenu, iCopy );
  228.         DisableItem( myMenu, iPaste );
  229.         DisableItem( myMenu, iClear );
  230.     }
  231. }
  232.  
  233.  
  234.  
  235. /*****************************************************************************/
  236. //    AppleEvent-handling Routines
  237. /*****************************************************************************/
  238.  
  239. //
  240. //    DoOpenApp
  241. //
  242. pascal OSErr DoOpenApp( AppleEvent theAppleEvent, AppleEvent reply, long refCon )
  243. {
  244.     //    This handler gets called when the user double-clicks on the icon.
  245.     //    It does not get called when the user drops something on the icon.
  246.     ShowAboutBox();
  247.     return ( noErr );
  248. }
  249.  
  250.  
  251. //
  252. //    DoOpenDoc
  253. //
  254. pascal OSErr DoOpenDoc( const AppleEvent *theAppleEvent, const AppleEvent *reply, long refCon )
  255. {
  256.     OSErr        err;
  257.     AEDescList    docList;
  258.     long        count, index;
  259.     AEKeyword    keyword;
  260.     DescType    type;
  261.     FSSpec        file;
  262.     Size        actualSize;
  263.     
  264.     InitializeStuff();
  265.     
  266.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
  267.     if ( err == noErr )
  268.     {
  269.         if ( AECountItems( &docList, &count ) == noErr )
  270.         {
  271.             WorkMenus();    //    Disable most stuff in menus.
  272.             
  273.             for ( index = 1; index <= count; index++ )
  274.             {
  275.                 if ( AEGetNthPtr( &docList, index, typeFSS, &keyword, &type,
  276.                             &file, sizeof( file ), &actualSize ) == noErr )
  277.                 {
  278.                 
  279.     //    A little bit of disturbing news.  If the user drops an alias on my
  280.     //    icon, 'file' refers to the TARGET file - the Finder resolves the alias
  281.     //    automatically.  From what I can tell, there's no way for me to
  282.     //    determine if what was dropped on me was really an alias.
  283.     
  284.                     if ( IsAFolder( &file ) )    //    it is a folder
  285.                         DoFolderSelection( file );
  286.                     else    //    it is a file
  287.                         DoFileSelection( file );
  288.                 }
  289.                 else
  290.                     DisplayAlert( "\pERROR: Could not get AppleEvent data." );
  291.             }
  292.             
  293.             WaitMenus();    //    Set menus back to normal.
  294.         }
  295.         else
  296.         {
  297.             err = AEDisposeDesc( &docList );
  298.             DisplayFatalError( "\pFATAL ERROR: Could not get AppleEvents data." );
  299.         }
  300.     }
  301.     else
  302.         DisplayFatalError( "\pFATAL ERROR: Could not get AppleEvents data." );
  303.         
  304.     DeInitializeStuff();
  305.     gDone = true;    //    quit when we're done
  306.     return ( noErr );
  307. }
  308.  
  309.  
  310. //
  311. //    DoPrintDoc
  312. //
  313. pascal OSErr DoPrintDoc( AppleEvent theAppleEvent, AppleEvent reply, long refCon )
  314. {
  315.     return errAEEventNotHandled;
  316. }
  317.  
  318.  
  319. //
  320. //    DoQuitApp
  321. //
  322. pascal OSErr DoQuitApp( AppleEvent theAppleEvent, AppleEvent reply, long refCon )
  323. {
  324.     gDone = true;
  325.     return ( noErr );
  326. }
  327.  
  328.  
  329.  
  330. /*****************************************************************************/
  331. //    Event-handling Routines
  332. /*****************************************************************************/
  333.  
  334. //
  335. //    EventLoop
  336. //
  337. void    EventLoop( void )
  338. {
  339.     EventRecord        event;
  340.     
  341.     gDone = false;
  342.     gRecurseDir = true;    //    This should be stored in preferences file, actually
  343.     
  344.     while (gDone == false )
  345.     {
  346.         if (WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  347.             DoEvent( &event );
  348.     }
  349. }
  350.  
  351.  
  352. //
  353. //    DoEvent
  354. //
  355. void    DoEvent( EventRecord *eventPtr )
  356. {
  357.     char    theChar;
  358.     
  359.     switch ( eventPtr->what )
  360.     {
  361.         case kHighLevelEvent:
  362.             AEProcessAppleEvent( eventPtr );
  363.             break;
  364.         case mouseDown:
  365.             HandleMouseDown( eventPtr );
  366.             break;
  367.         case keyDown:
  368.         case autoKey:
  369.             theChar = eventPtr->message & charCodeMask;
  370.             if ( (eventPtr->modifiers & cmdKey) != 0)
  371.                 HandleMenuChoice( MenuKey( theChar ) );
  372.             break;
  373.     }
  374. }
  375.  
  376.  
  377. //
  378. //    HandleMouseDown
  379. //
  380. void    HandleMouseDown( EventRecord *eventPtr )
  381. {
  382.     WindowPtr    window;
  383.     short        thePart;
  384.     long        menuChoice;
  385.     
  386.     thePart = FindWindow( eventPtr->where, &window );
  387.     
  388.     switch ( thePart )
  389.     {
  390.         case inMenuBar:
  391.             menuChoice = MenuSelect( eventPtr->where );    //    do the menu thing
  392.             HandleMenuChoice( menuChoice );
  393.             break;
  394.         case inSysWindow:
  395.             SystemClick( eventPtr, window );
  396.             break;
  397.     }
  398. }
  399.  
  400.  
  401. //
  402. //    HandleMenuChoice
  403. //
  404. void    HandleMenuChoice( long    menuChoice )
  405. {
  406.     short    menu;
  407.     short    item;
  408.     
  409.     if ( menuChoice != 0 )
  410.     {
  411.         menu = HiWord( menuChoice );
  412.         item = LoWord( menuChoice );
  413.         
  414.         switch ( menu )
  415.         {
  416.             case mApple:
  417.                 HandleAppleChoice( item );
  418.                 break;
  419.             case mFile:
  420.                 HandleFileChoice( item );
  421.                 break;
  422.             case mEdit:
  423.                 //    Edit menu is there only for the sake of DAs...
  424.                 break;
  425.         }
  426.         HiliteMenu( 0 );
  427.     }
  428. }
  429.  
  430.  
  431. //
  432. //    HandleAppleChoice
  433. //
  434. void    HandleAppleChoice( short item )
  435. {
  436.     MenuHandle    appleMenu;
  437.     Str255        accName;
  438.     short        accNumber;
  439.     
  440.     switch ( item )
  441.     {
  442.         case iAbout:
  443.             ShowAboutBox();
  444.             break;
  445.         default:
  446.             appleMenu = GetMHandle( mApple );
  447.             GetItem( appleMenu, item, accName );
  448.             accNumber = OpenDeskAcc( accName );
  449.             break;
  450.     }
  451. }
  452.  
  453.  
  454. //
  455. //    HandleFileChoice
  456. //
  457. void    HandleFileChoice( short item )
  458. {
  459.     switch ( item )
  460.     {
  461.         case iOpen:
  462.             SelectFolder();    //    Do the open file stuff...
  463.             break;
  464.         case iPrefs:
  465.             //    The preferences stuff is currently unimplemented...
  466.             break;
  467.         case iQuit:
  468.             gDone = true;
  469.             break;
  470.     }
  471. }
  472.  
  473.  
  474. void    SelectFolder( void )
  475. {
  476.     StandardFileReply    reply;
  477.     OSErr                err;
  478.     Str255                returnValue;
  479.     
  480.     err = CustomGetFolder( &reply );
  481.     if ( err == noErr )
  482.     {
  483.         if ( reply.sfGood )    //    User did not hit cancel.
  484.         {
  485.             WorkMenus();    //    Disable most stuff in menus.
  486.             
  487.             if ( InitializeStuff() )
  488.             {
  489.                 if ( IsAFolder( &(reply.sfFile) ) )
  490.                     DoFolderSelection( reply.sfFile );
  491.                 else
  492.                     DoFileSelection( reply.sfFile );
  493.                 
  494.                 DeInitializeStuff();
  495.             }
  496.             
  497.             WaitMenus();    //    Set menus back to normal.
  498.         }
  499.     }
  500.     else
  501.     {
  502.         ResolveOSErr( err, returnValue );
  503.         DisplayAlert( returnValue );
  504.     }
  505. }
  506.  
  507.  
  508. //
  509. //    DoFileSelection
  510. //
  511. void    DoFileSelection( FSSpec selSpec )
  512. {
  513.     BusyCursor();    //    Set the cursor to the watch.
  514.     
  515.     DoToEachFile( selSpec );
  516.     
  517.     SetCursor( &qd.arrow );    //    Set the cursor back to the arrow
  518. }
  519.  
  520.  
  521. //
  522. //    DoFolderSelection
  523. //
  524. void    DoFolderSelection( FSSpec selSpec )
  525. {
  526.     BusyCursor();    //    Set the cursor to the watch.
  527.  
  528.     DoAllFiles( selSpec );    //    Do the recursive stuff.
  529.     DoToSelFolder( selSpec );
  530.     
  531.     SetCursor( &qd.arrow );    //    Set the cursor back to the arrow
  532. }
  533.  
  534.